package com.dht.xiao.jianye.activity_bed;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.dht.xiao.jianye.R;
import com.dht.xiao.jianye.base.BaseActivity;
import com.dht.xiao.jianye.bean.SimpleBean;
import com.dht.xiao.jianye.bean.newdata.LoginNewBean;
import com.dht.xiao.jianye.http.MainHttp;
import com.dht.xiao.jianye.utils.UserHelper;
import com.trello.rxlifecycle2.android.ActivityEvent;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.ToastUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

//被服柜 投递

public class LoginBedActivity extends BaseActivity {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    int type;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_bed_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bed_login);
        ButterKnife.bind(this);
        mTitleBar.leftExit(this);
        type = getIntent().getIntExtra("name",0);
        if (type == 1) {
            mTitleBar.setTitle("投递");
        }else if (type == 2){
            mTitleBar.setTitle("领取");
        }else if (type == 3){
            mTitleBar.setTitle("查询");
        }else if (type == 4){
            mTitleBar.setTitle("绑定标签");
        }
    }


    private void initData() {
        final String username = etPhone.getText().toString();
        if (TextUtils.isEmpty(username)) {
            ToastUtil.show(this, R.string.please_input_username);
            etPhone.requestFocus();
            return;
        }
        final String password = etPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(this, R.string.please_input_password);
            etPassword.requestFocus();
            return;
        }
       // showWaitingDialog("请稍等", false);

       login(username,password);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick(R.id.btn_account_login)
    public void onViewClicked() {
        initData();
        //login("13800138000","11111111","");
    }

    private void login(String account,String password) {
        MainHttp.get().login(account,password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new OkObserver<LoginNewBean>() {
                    @Override
                    public void onSuccess(LoginNewBean hotelBean) {
                        //showOneToast(hotelBean.msg);
                        UserHelper.get().saveUserInfo(hotelBean.data.sign);
                        if (type == 1) {
                            startActivity(new Intent(LoginBedActivity.this, BedDeliverActivity.class));
                        }else if (type == 2){
                            startActivity(new Intent(LoginBedActivity.this, BedGetActivity.class));
                        }else if (type == 3){
                            startActivity(new Intent(LoginBedActivity.this, BedQueryActivity.class));
                        }
                        else if (type == 4){
                            startActivity(new Intent(LoginBedActivity.this, BindTagActivity.class));
                        }
                        finishActivity();
                    }

                    @Override
                    public void onFailure(String message, Throwable tr) {
                        ToastUtil.show(getActivity(), message);
                    }
                });
    }
}

